<template>
<!-- 鼠标移入停住轮播图，移出开启轮播图 -->
  <div class='xtx-carousel'  @mouseenter="teriemend" @mouseleave="lbt" >
    <ul class="carousel-body">
      <!-- 遍历传过来的数据 ，fade是淡出淡入的动画类名-->
      <li class="carousel-item"  v-for="i,a in sliders||[]" :key="i.id" :class="{'fade':index===a}">
        <RouterLink to="/" v-if="i.imgUrl">
          <img :src="i.imgUrl"  alt="">
        </RouterLink>
        <div class="slider" v-else>
           <RouterLink v-for="goods in i" :key="goods.id" :to="`/product/${goods.id}`">
            <img :src="goods.picture" alt="">
            <p class="name ellipsis">{{goods.name}}</p>
            <p class="price">&yen;{{goods.price}}</p>
          </RouterLink>
        </div>

      </li>
    </ul>
    <!-- 点击执行prev函数 -->
    <a href="javascript:;" class="carousel-btn prev" @click="prev"><i class="iconfont icon-angle-left" ></i></a>
    <!-- 点击执行next函数 -->
    <a href="javascript:;" class="carousel-btn next" @click="next"><i class="iconfont icon-angle-right" ></i></a>
    <div class="carousel-indicator">
      <!-- 遍历传过来的数组生成小圆点，如果下标与显示的图片的对应一致则高亮 -->
      <span v-for="v,c in sliders" :key="v.id" @click="index=c" :class="{'active':index==c}"></span>
    </div>
  </div>
</template>

<script>
// onUnmounted是销毁组件后的钩子函数
import { ref, onUnmounted, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 接收传过来的数据，类型为数组
    sliders: {
      type: Array,
      default: () => []
    },
    duration: {
      // 每次轮播的显示时间
      type: Number,
      default: 3000
    },
    autoPlay: {
      // 是否自动轮播
      type: Boolean,
      default: true
    }
  },
  // 接收props，用于修获取props的数据
  setup (props) {
    console.log(props.sliders)
    // 定义一个显示图片的下标
    const index = ref(0)
    let teriem = null
    // 清理定时器的函数
    const teriemend = () => {
      clearInterval(teriem)
    }
    // 轮播图的函数
    const lbt = () => {
      if (teriem) {
        // 如果之前的轮播图还存在就清理之前的
        teriemend()
      }
      // teriem就是该定时器的名字
      teriem = setInterval(() => {
        // 定义好的图片下标吗每次+1
        index.value++
        // 如果下标===传过来的数组长度则下标等于0
        if (index.value === props.sliders.length) {
          index.value = 0
        }
      }, props.duration)
    }
    // 如果有数据与允许执行则执行
    watch(() => {
      if (props.sliders.length && props.autoPlay) {
        console.log('轮播图启动', props.sliders.length)
        lbt()
      }
    })

    // 调用perve函数index--，如果到达边界则等于数组的长度
    const prev = () => {
      if (index.value === 0) {
        index.value = props.sliders.length - 1
      } else {
        index.value--
      }
    }
    // 调用next则index++，如果到达边界则index=0
    const next = () => {
      if (index.value === props.sliders.length - 1) {
        index.value = 0
      } else {
        index.value++
      }
    }
    // 组件销毁后也清理定时器
    onUnmounted(() => {
      clearInterval(teriem)
    })
    return { index, prev, next, teriem, teriemend, lbt }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel{
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel{
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0,0,0,0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background:  #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0,0,0,.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 1s linear;
      &.prev{
        left: 20px;
      }
      &.next{
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
.slider {
  display: flex;
  justify-content: space-around;
  padding: 0 40px;
  > a {
    width: 240px;
    text-align: center;
    img {
      padding: 20px;
      width: 230px!important;
      height: 230px!important;
    }
    .name {
      font-size: 16px;
      color: #666;
      padding: 0 40px;
    }
    .price {
      font-size: 16px;
      color: @priceColor;
      margin-top: 15px;
    }
  }
}
</style>
